﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DelonixRegiaHMSys.Core.Interfaces;
using DelonixRegiaHMSys.Core.Models;

namespace DelonixRegiaHMSys.Infrastructure.Data
{
    public class EfGuestParticularQuery : IGuestParticularQuery
    {
        public IEnumerable<GuestParticular> GetAll()
        {
            var context = new hmsDBEntities();
            return context.guestDetails.Select(g => new GuestParticular()
            {
                GuestID = g.guestID,
                GuestLastName = g.guestLastName,
                GuestFirstName = g.guestFirstName,
                HomeAddress = g.homeAddress,
                Nationality = g.nationality,
                ContactNum = g.contactNum,
                EmailAddress = g.emailAddress,
                MailAddress = g.mailAddress
            });
        }

        public GuestParticular GetGuestbyID(string guestID)
        {
            var context = new hmsDBEntities();
            var guestToRetrieve = context.guestDetails.FirstOrDefault(d => d.guestID == guestID);
            GuestParticular gp = new GuestParticular();
            gp.GuestID = guestToRetrieve.guestID;
            gp.GuestLastName = guestToRetrieve.guestLastName;
            gp.GuestFirstName = guestToRetrieve.guestFirstName;
            gp.HomeAddress = guestToRetrieve.homeAddress;
            gp.Nationality = guestToRetrieve.nationality;
            gp.ContactNum = guestToRetrieve.contactNum;
            gp.EmailAddress = guestToRetrieve.emailAddress;
            gp.MailAddress = guestToRetrieve.mailAddress;
            return gp;
        }

        public void AddGuest(GuestParticular gp)
        {
            var context = new hmsDBEntities();
            var newGuest = context.guestDetails.Create();
            newGuest.guestID = gp.GuestID;
            newGuest.guestLastName = gp.GuestLastName;
            newGuest.guestFirstName = gp.GuestFirstName;
            newGuest.homeAddress = gp.HomeAddress;
            newGuest.nationality = gp.Nationality;
            newGuest.contactNum = gp.ContactNum;
            newGuest.emailAddress = gp.EmailAddress;
            newGuest.mailAddress = gp.MailAddress;
            context.guestDetails.Add(newGuest);
            context.SaveChanges();
        }
        public void DeleteGuest(string guestID)
        {
            var context = new hmsDBEntities();
            var guestToDelete = context.guestDetails.FirstOrDefault(d => d.guestID == guestID);
            context.guestDetails.Remove(guestToDelete);
            context.SaveChanges();
        }
        public void UpdateGuest(GuestParticular gp)
        {
            var context = new hmsDBEntities();
            var guestToUpdate = context.guestDetails.FirstOrDefault(up => up.guestID == gp.GuestID);
            guestToUpdate.guestLastName = gp.GuestLastName;
            guestToUpdate.guestFirstName = gp.GuestFirstName;
            guestToUpdate.homeAddress = gp.HomeAddress;
            guestToUpdate.nationality = gp.Nationality;
            guestToUpdate.contactNum = gp.ContactNum;
            guestToUpdate.emailAddress = gp.EmailAddress;
            guestToUpdate.mailAddress = gp.MailAddress;
            context.SaveChanges();
        }

        public bool GuestExist(string guestID)
        {
            var context = new hmsDBEntities();
            var guestExist = context.guestDetails.FirstOrDefault(gp => gp.guestID == guestID);
            if (guestExist != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
